
#include "module.h"
#include "queue.h"
#include <stdio.h>


void module_init_ops(MODULE_t *mod, Ops_t *ops)
{
    mod->Operations = ops;
}

void module_init_pm_ops(MODULE_t *mod, PM_Ops_t *pm_ops)
{
    mod->PM_Operations = pm_ops;
}

int insmod(MODULE_t *mod)
{
    if (mod->name)
    {
        printf("insmod flow: the name is[%s]\r\n", mod->name);
    }
    else
    {
        printf("insmod flow: the name can not be NULL\r\n");
        return -1;
    }
    
    mod->MoudleId = 1;
    mod->state = STEP_START;
    return add((void*)mod);
}

int rmmod(MODULE_t *mod)
{
    return del(mod);
}

/* for msg control */
int post_msg(int pid, void *msg, unsigned int len)
{
    MODULE *mod = (MODULE *)get(pid);
    int ii;
    printf("->post to %s\r\n", mod->name);
    if (mod == NULL || msg == NULL)
        return -1;
    
    
    MSG_t mg;
    for (ii=0; ii<32; ii ++)
    { 
        if (mod->ModMsg[ii].MsgLen == 0)
        {
            mg.from = 0;
            mg.MsgLen = len;
            mg.pMSG = msg;
            memcpy((void *)&mod->ModMsg[ii], (void*)&mg, sizeof (MSG_t));
            return 0;
        }
    }
    return -1;
}

int get_msg(MODULE *mod, MSG_t *msg)
{
    int ii;
    
    if (mod == NULL)
        return -1;
    
    for (ii=0; ii<32; ii ++)
    { 
        if (mod->ModMsg[ii].MsgLen != 0)
        {
            memcpy((void*)msg, (void *)&mod->ModMsg[ii], sizeof (MSG_t));
            mod->ModMsg[ii].MsgLen = 0;
            return 1;
        }
    }
    return 0;
}
